/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved.
 */
package com.kaaction.modules.eb.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.kaaction.common.config.Global;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.eb.entity.CommerceClassify;
import com.kaaction.modules.eb.service.CommerceClassifyService;
import com.kaaction.modules.sys.entity.Menu;

/**
 * 商品分类Controller
 * @author huanghaiming
 * @version 2017-04-11
 */
@Controller
@RequestMapping(value = "${adminPath}/eb/commerceClassify")
public class CommerceClassifyController extends BaseController {

	@Autowired
	private CommerceClassifyService commerceClassifyService;
	
	@ModelAttribute
	public CommerceClassify get(@RequestParam(required=false) String id) {
		CommerceClassify entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = commerceClassifyService.get(id);
		}
		if (entity == null){
			entity = new CommerceClassify();
		}
		return entity;
	}
	
	@RequiresPermissions("eb:commerceClassify:view")
	@RequestMapping(value = {"list", ""})
	public String list(CommerceClassify commerceClassify, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<CommerceClassify> list = Lists.newArrayList();
		List<CommerceClassify> sourcelist = commerceClassifyService.findAllCommerceClassify();
		CommerceClassify.sortList(list, sourcelist, Menu.getRootId(), true);
        model.addAttribute("list", list);
		return "modules/eb/commerceClassifyList";
	}

	@RequiresPermissions("eb:commerceClassify:view")
	@RequestMapping(value = "form")
	public String form(CommerceClassify commerceClassify, Model model) {
		
		if (commerceClassify.getParent()==null||commerceClassify.getParent().getId()==null){
			commerceClassify.setParent(new CommerceClassify(CommerceClassify.getRootId()));
		}
		commerceClassify.setParent(commerceClassifyService.get(commerceClassify.getParent().getId()));
		model.addAttribute("classify", commerceClassify);
		return "modules/eb/commerceClassifyForm";
	}

	@RequiresPermissions("eb:commerceClassify:edit")
	@RequestMapping(value = "save")
	public String save(CommerceClassify commerceClassify, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, commerceClassify)){
			return form(commerceClassify, model);
		}
		commerceClassifyService.save(commerceClassify);
		addMessage(redirectAttributes, "保存商品分类成功");
		return "redirect:"+Global.getAdminPath()+"/eb/commerceClassify/?repage";
	}
	
	@RequiresPermissions("eb:commerceClassify:edit")
	@RequestMapping(value = "delete")
	public String delete(CommerceClassify commerceClassify, RedirectAttributes redirectAttributes) {
		commerceClassifyService.delete(commerceClassify);
		addMessage(redirectAttributes, "删除商品分类成功");
		return "redirect:"+Global.getAdminPath()+"/eb/commerceClassify/?repage";
	}
	
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response){
		
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<CommerceClassify> list = commerceClassifyService.findAllCommerceClassify();
		for (int i=0; i<list.size(); i++){
			CommerceClassify e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId())/* && e.getParentIds().indexOf(","+extId+",")==-1)*/)){
				
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("name", e.getName());
				mapList.add(map);
			}
		}
		return mapList;
	}

}